<script setup lang="ts" name="Login">
import { User, Lock } from '@element-plus/icons-vue'
import { reactive, ref } from 'vue'
import useUserStore from "@/store/modules/user";
import { useRouter, useRoute } from "vue-router";
import { ElMessage } from "element-plus";
import { getTime } from '@/utils/time';
import type { FormRules } from 'element-plus'
// 获取user仓库
let userStore = useUserStore()
// 获取路由器
let router = useRouter()
let $route = useRoute()
const loginForm = reactive({
    username: 'admin',
    password: '111111'
})
let loading = ref(false)

const loginFormRule = ref()
async function handleLogin() {
    await loginFormRule.value.validate();
    loading.value = true
    try {
        await userStore.userLogin(loginForm)
        //编程式导航跳转到展示数据首页
        // //判断登录的时候,路由路径当中是否有query参数，如果有就往query参数跳转，没有跳转到首页
        // let redirect: any = $route.query.redirect;   //说白了就是假如你在某个页面退出登录后再次登录需要回到当前页面，个人觉得登录还是只跳首页比较合理
        // router.push({ path: redirect || '/' });
        await userStore.getUserInfo()   //手动添加的 因为自己新增了pinia数据持久化
        router.push('/')
        ElMessage({
            type: 'success',
            message: `登录成功! ${getTime()}好~`
        })
        loading.value = false
    } catch (error) {
        loading.value = false
        ElMessage({
            type: 'error',
            message: (error as Error).message
        })
    }
}

//  自定义校验规则  账号
const validateUserName = (rule: any, value: any, callback: any) => {
    // rule：校验规则对象 value 表单输入框的值 callback 回调函数
    if (value.length >= 5) {
        callback()
    } else {
        callback(new Error('账号长度至少是五位数'))
    }
}
//  自定义校验规则  密码
const validatePassword = (rule: any, value: any, callback: any) => {
    // rule：规则 value 表单输入框的值 callback 回调函数
    if (value.length >= 6) {
        callback()
    } else {
        callback(new Error('账号长度至少是五位数'))
    }
}

// 表单校验规则
const loginRules = reactive<FormRules<typeof loginFormRule>>({
    //规则对象属性:
    //required,代表这个字段务必要校验的
    //min:文本长度至少多少位
    //max:文本长度最多多少位
    //message:错误的提示信息
    //trigger:触发校验表单的时机 change->文本发生变化触发校验,blur:失去焦点的时候触发校验规则
    username: [
        { trigger: 'change', validator: validateUserName }
    ],
    password: [
        { trigger: 'change', validator: validatePassword }
    ]
})




</script>
<template>
    <div class="login_container">
        <el-row>
            <el-col :span="12" :xs="0"></el-col>
            <el-col :span="12" :xs="24">
                <el-form ref="loginFormRule" :model="loginForm" :rules="loginRules" class="login_form">
                    <h1>hello</h1>
                    <h2>欢迎来到硅谷甄选</h2>
                    <el-form-item label="" prop="username">
                        <el-input :prefix-icon="User" v-model="loginForm.username"></el-input>
                    </el-form-item>
                    <el-form-item label="" prop="password">
                        <el-input :prefix-icon="Lock" type="password" show-password
                            v-model="loginForm.password"></el-input>
                    </el-form-item>
                    <el-form-item label="">
                        <el-button :loading="loading" class="login_btn" type="primary" size="default"
                            @click="handleLogin">登录</el-button>
                    </el-form-item>
                </el-form>
            </el-col>
        </el-row>
    </div>
</template>
<style scoped lang="scss">
.login_container {
    width: 100%;
    height: 100vh;
    background: url('@/assets/images/background.jpg') no-repeat;
    background-size: cover;

    .login_form {
        position: relative;
        width: 80%;
        top: 30vh;
        background: url('@/assets/images/login_form.jpg') no-repeat;
        background-size: cover;
        padding: 40px;

        h1 {
            color: white;
            font-size: 40px;
        }

        h2 {
            font-size: 20px;
            color: white;
            margin: 20px 0px;
        }

        .login_btn {
            width: 100%;
        }
    }
}
</style>